package bridge;


import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;

public class Controller {

	private static Runtime runtime;
	private static Process process;
	private static String path = System.getProperty("user.home") + "/util";



	/**
	 * applica l'algoritmo di type inference al termine preso in input e poi
	 * richiama il risolutore di vincoli .
	 * @param term termine
	 * @throws IOException
	 * @throws InterruptedException
	 */
	public static void infDlal(String term) throws IOException,
			InterruptedException {
		runtime = Runtime.getRuntime();
		term = "\"" + term + ";;\"";

		process = runtime.exec(new String[] {
				"bash",
				"-c",
				"cd " + path + "/typeDlal && ./dlalscript.sh constraint  type "
						+ term });
	}

	public static String lciInterpreter(String term) throws IOException,
			InterruptedException {
		runtime = Runtime.getRuntime();
		term = " \"" + term + "\"";
		File res = new File(path + "/lci/interpResult");
		if (res.exists())
			res.delete();

		process = runtime.exec(new String[] { "bash", "-c",
				"cd " + path + "/lci && ./lciscript.sh  " + term });

		InputStream inp = process.getInputStream();
		String temp = "";
		int n = 0;
		boolean done = false;
		while ((n = inp.read()) != -1) {
			if (done)
				temp += "" + (char) n;
			if ((char) n == '>')
				done = true;
		}
		return temp;
	}
	
	public static HashMap<String,Double> getVarValue(){
		HashMap<String, Double> map = new HashMap<String, Double>();
		String userhome = System.getProperty("user.home");
		GlpkSolver solver = new GlpkSolver();
		GlpkSolver glp = solver.readCpxlp(userhome+"/util/typeDlal/constraint");
		glp.integer();
		for(int i=1; i<=glp.getNumCols(); i++){
			map.put(glp.getColName(i), glp.mipColVal(i));		
		}			
		return map;
	}
	

}